<?php

namespace App\library\services;

use App\library\services\lib\DelayQueue;
use App\library\common\Sequence;
use App\Models\Order;
use App\Models\UserRelease;
use App\Models\UserReleaseImages;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class OrderService extends BaseServices
{
    /**
     * Author:Zhongyuan Lu
     * 利用雪花算法生成订单号
     * @return mixed
     */
    public static function getOrderNo()
    {
        $snowflake = app('snowflake');
        $snowflake->setSequenceResolver(new Sequence());
        return $snowflake->id();
    }

    /**
     * Author:Zhongyuan Lu
     * 创建发布记录 同时 生成订单
     * @param $uid
     * @param $releaseId
     * @param $type
     * @return array
     */
    public static function createOrder($uid, $releaseId, $type)
    {
        $ret = [
            'order_no' => self::getOrderNo(),
            'uid' => $uid,
            'release_id' => $releaseId,
            'type' => $type,
            //手续费
            'price' => ConfigService::getValue('release.serviceCharge'),
        ];
        //订单创建
        $order_id = Order::create($ret)->id;
        if ($order_id) {
            return self::success('订单创建成功',$order_id);
        }
        return self::fail('订单创建失败');
    }

    /**
     * Author:YangXinPeng
     * 订单超时处理 生成者
     * @param $orderId
     */
    public static function queOrder($orderId)
    {
        $delay = new DelayQueue();
        $ttl = 1000 * 60 * 30;//订单30m后超时
        $delayExName = 'delay-order-exchange';//超时exchange
        $delayQueueName = 'delay-order-queue';//超时queue
        $queueName = 'ttl-order-queue';//订单queue
        $delay->createQueue($ttl, $delayExName, $delayQueueName, $queueName);
        $data = [
            'order_id' => $orderId,
        ];
        $delay->sendMessage(json_encode($data), $queueName);
        sleep(1);
    }

    /**
     * Author:YangXinPeng
     * 订单延时队列消费者
     * @throws \ErrorException
     */
    public static function consumeOrder()
    {
        $delay = new DelayQueue();
        $delayQueueName = 'delay-order-queue';
        $callback = function ($msg) {
            $val = json_decode($msg->body, true);
            $orderId = $val['order_id'];
            $order = Order::find($orderId);
            //status 支付状态  1 已支付 2 已失效
            if ($order->status != 1) {
                $order->status = 2;
            }
            $order->save();
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            sleep(1);
        };
        /**
         * 消费已经超时的订单信息，进行处理
         */
        $delay->consumeMessage($delayQueueName, $callback);
    }

    /**
     * Author:Zhongyuan Lu
     * 获取订单列表
     * @param $page
     * @param $limit
     * @param $uid
     * @return array
     */
    public static function getOrderList($page, $limit, $uid)
    {
        $data = Order::where('id',$uid)->paginate($limit);
        $data && $data = $data->toArray();
        return self::success('success',$data);
    }
}
